﻿Public Class GunlukEmanetListesi : Inherits Process

	Dim islemtar As Input

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Günlük emanet listesi", "İşlem tarihini girin.")
				islemtar = AddDate("islemtar", "İşlem tarihi", "nl", YMD(Today))
				StartBody()
				AddList("List", 0, 300)
				List.AddColumns("yil(Yıl;70c),tahsilno(Tahsil no.;70r),makbuzmik(Toplam makbuz;100m),tediyemik(Toplam tediye;100m),gunbasi(Gün başı;100m),kalan(Kalan;100m)")
				AddReport()
			Case EventType.ValidateForm
				Rs = Kasa.Emanet.GunlukListe(Local, islemtar.Get)
				Dim Emanetler As New List(Of Emanet)
				For Each Row In Rs.GroupBy("tahsiltar,tahsilno")
					Emanetler.Add(New Emanet(Rs.Select("tahsiltar='" & YMDS(Row("tahsiltar")) & "' and tahsilno=" & Row("tahsilno"))))
				Next
				Dim d1 As Date = islemtar.Get
				Dim d2 = d1.AddDays(1)
				Dim Result =
				 From em In Emanetler
				 Let makbuzmik = (From i In em.Islemler Where i.islemtar >= d1 And i.islemtar < d2 And Not i.tediye Select i.miktar).Sum
				 Let tediyemik = (From i In em.Islemler Where i.islemtar >= d1 And i.islemtar < d2 And i.tediye Select i.miktar).Sum
				 Let gunbasi = (From i In em.Islemler Where i.islemtar >= d1 Order By i.islemtar Select i.islemoncemik).FirstOrDefault
				 Let kalan = gunbasi - tediyemik - makbuzmik
				 Order By em.tahsilno
				For Each Row In Result
					List.AddRow(Row.em.yil, Row.em.tahsilno, Row.makbuzmik, Row.tediyemik, Row.gunbasi, Row.kalan)
				Next
				Buttons.Show("Reset")
				e.Result = True
		End Select
	End Sub

End Class

Class Emanet

	Public tarih As Date
	Public tahsilno As Integer
	Public miktar As Decimal
	Public Islemler As New List(Of Islem)

	Sub New(Rs As Recordset)
		Open(Rs)
	End Sub

	ReadOnly Property yil As Integer
		Get
			Return tarih.Year
		End Get
	End Property

	Sub Open(Rs As Recordset)
		Dim Kalan As Decimal
		For Each Row In (From r In Rs.Rows Order By r("islemtar"))
			If IsNull(Row("makbuz")) And IsNull(Row("tediyeno")) Then
				tarih = Row("tahsiltar")
				tahsilno = Row("tahsilno")
				Kalan = Row("emanmikt")
			ElseIf IsNull(Row("iptal"), "") = "P" Then
				Dim Islem As New Islem
				Islem.iptal = True
				Islem.islemoncemik = Kalan
				Islem.islemtar = Row("islemtar")
				Islem.makbuzno = Row("makbuz")
				Islem.miktar = Row("makbuzmik")
				Kalan += Islem.miktar
				Islem.kalan = Kalan
				Islemler.Add(Islem)
			ElseIf Not IsNull(Row("makbuz")) Then
				Dim Islem As New Islem
				Islem.islemoncemik = Kalan
				Islem.islemtar = Row("islemtar")
				Islem.makbuzno = Row("makbuz")
				Islem.miktar = Row("makbuzmik")
				Kalan -= Islem.miktar
				Islem.kalan = Kalan
				Islemler.Add(Islem)
			ElseIf Not IsNull(Row("tediyeno")) Then
				Dim Islem As New Islem
				Islem.islemoncemik = Kalan
				Islem.islemtar = Row("islemtar")
				Islem.tediye = True
				Islem.tediyeno = Row("tediyeno")
				Islem.miktar = Kalan - Row("emanmikk")
				Kalan = Row("emanmikk")
				Islem.kalan = Kalan
				Islemler.Add(Islem)
			End If
		Next
	End Sub

	Class Islem

		Public islemtar As Date
		Public islemoncemik As Decimal
		Public tediye As Boolean
		Public makbuzno As Integer
		Public tediyeno As Integer
		Public miktar As Decimal
		Public kalan As Decimal
		Public iptal As Boolean

	End Class

End Class